class DeportistasController < ApplicationController		
			 	
  def index
    list
    render :action => 'list'
	
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
  	if params[:notice] != nil		 
		flash[:notice] = params[:notice]
	else
		flash[:notice] = "Operacion realizada con extio"
	end
	  	
    @deportista_pages, @deportistas = paginate :deportistas, :per_page => 10
  end

  def show
    @deportista = Deportista.find(params[:id])
  end

  def new  	
    @deportista = Deportista.new
  end

  def create
  	
    @deportista = Deportista.new(params[:deportista])
	@deportista.edad = (Time.now.year - @deportista.fecha_nac.year)
    if Deportista.exists?(:identificacion => @deportista.identificacion)
    	flash[:notice] = 'Failure to create a Deportista'
		session[:notice] = 'Failure to create a Deportista'
    	redirect_to :action => 'list', :notice => 'Failure to create a Deportista'
    else
    	if @deportista.save
      		flash[:notice] = 'Deportista was successfully created.'
      		redirect_to :action => 'list'
    	else
      		render :action => 'new'
    	end
    end
  end

  def edit
    @deportista = Deportista.find(params[:id])
  end

  def update
    @deportista = Deportista.find(params[:id])
    if @deportista.update_attributes(params[:deportista])
      flash[:notice] = 'Deportista was successfully updated.'
      redirect_to :action => 'show', :id => @deportista
    else
      render :action => 'edit'
    end
  end

  def destroy
    Deportista.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
	  
  def actualizar_equipo_por_delegacion 
  	@equipo_delegacion = Delegacion.find(params[:deportista_delegacion_id]).equipos
  end
end
